package indi.kider.server.employee.api;

import indi.kider.common.model.employee.LeaveLog;
import indi.kider.server.employee.service.LeaveLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by shixj on 2018/4/26.
 */
@RestController
@RequestMapping("leaveLog")
public class LeaveLogController {
    @Autowired
    private LeaveLogService tenantService;

    /**
     * @api {GET} /{id}
     * @apiGroup leaveLog
     * @apiDescription 根据id获取请假记录信息
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/leaveLog/1
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"id":1,"employee":1,"leaveTime":"2018-04-26 13:46:40","fakeTime":"2018-05-04 13:46:43","cause":"21","lenOfTime":2.0,"actualLenOfTIme":2.0,"timeUnit":"2"}}
     * */
    @GetMapping("{id}")
    public CommonResponse<LeaveLog> getLeaveLogById(@PathVariable Long id) {
        return CommonResponse.buildGetSuccessResponse(tenantService.getLeaveLogById(id));
    }
    /**
     * @api {POST} /list
     * @apiGroup leaveLog
     * @apiDescription 根据字段查询获取请假记录列表（分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/leaveLog/list
     * {"num":"0","size":"8","query":{"employee":1,"leaveTime":"2018-04-26 13:46:40","fakeTime":"2018-05-04 13:46:43","cause":"21","lenOfTime":2.0,"actualLenOfTIme":2.0,"timeUnit":"2"}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"id":1,"employee":1,"leaveTime":"2018-04-26 13:46:40","fakeTime":"2018-05-04 13:46:43","cause":"21","lenOfTime":2.0,"actualLenOfTIme":2.0,"timeUnit":"2"}],"total":1,"size":0}}
     * */
    @PostMapping("/list")
    public CommonResponse getLeaveLogPages(@RequestBody PageRequest<LeaveLog> request) {
        return CommonResponse.buildGetSuccessResponse(tenantService.getLeaveLogPages(request));
    }
    /**
     * @api {POST} /list/all
     * @apiGroup leaveLog
     * @apiDescription 根据字段查询获取请假记录列表（不分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/leaveLog/list/all
     * {"employee":1,"leaveTime":"2018-04-26 13:46:40","fakeTime":"2018-05-04 13:46:43","cause":"21","lenOfTime":2.0,"actualLenOfTIme":2.0,"timeUnit":"2"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":[{"id":1,"employee":1,"leaveTime":"2018-04-26 13:46:40","fakeTime":"2018-05-04 13:46:43","cause":"21","lenOfTime":2.0,"actualLenOfTIme":2.0,"timeUnit":"2"}]}
     * */
    @PostMapping("/list/all")
    public CommonResponse getLeaveLogs(@RequestBody LeaveLog leaveLog) {
        return CommonResponse.buildGetSuccessResponse(tenantService.getLeaveLogs(leaveLog));
    }
    /**
     * @api {POST} /
     * @apiGroup leaveLog
     * @apiDescription 新增请假记录
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/leaveLog
     * {"employee":1,"leaveTime":"2018-04-26 13:46:40","fakeTime":"2018-05-04 13:46:43","cause":"21","lenOfTime":2.0,"actualLenOfTIme":2.0,"timeUnit":"2"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"201","msg":"保存成功！","data":{"id":1,"employee":1,"leaveTime":"2018-04-26 13:46:40","fakeTime":"2018-05-04 13:46:43","cause":"21","lenOfTime":2.0,"actualLenOfTIme":2.0,"timeUnit":"2"}}
     */
    @PostMapping("")
    public CommonResponse add(@RequestBody LeaveLog leaveLog, HttpServletRequest request) {
        return CommonResponse.buildAddSuccessResponse(tenantService.add(leaveLog));
    }
    /**
     * @api {PUT} /
     * @apiGroup leaveLog
     * @apiDescription 修改请假记录
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/leaveLog
     * {"id":1,"employee":1,"leaveTime":"2018-04-26 13:46:40","fakeTime":"2018-05-04 13:46:43","cause":"21","lenOfTime":3.0,"actualLenOfTIme":2.0,"timeUnit":"2"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"202","msg":"修改成功！","data":{"id":1,"employee":1,"leaveTime":"2018-04-26 13:46:40","fakeTime":"2018-05-04 13:46:43","cause":"21","lenOfTime":2.0,"actualLenOfTIme":3.0,"timeUnit":"2"}}
     */
    @PutMapping("")
    public CommonResponse modify(@RequestBody LeaveLog leaveLog) {
        return CommonResponse.buildUpdateSuccessResponse(tenantService.modify(leaveLog));
    }
    /**
     * @api {DELETE} /{id}
     * @apiGroup leaveLog
     * @apiDescription 删除请假记录
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/leaveLog/1
     * @apiSuccessExample {json} 返回样例:
     * {"status":"204","msg":"删除成功！","data":""}}
     */
    @DeleteMapping("/{id}")
    public CommonResponse delById(@PathVariable Long id, HttpServletRequest request) {
        tenantService.delById(id);
        return CommonResponse.buildDelSuccessResponse("");
    }
}
